home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / rend10.lzh / REND1.0 / sincos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-01  |  5.7 KB  |  214 lines

  1. /*
  2.  * Title:
  3.  *    sincos.c
  4.  *
  5.  * Authors:
  6.  *    Michael P. Schenck
  7.  *
  8.  * Purpose:
  9.  *    This demo shows a three dimensional function over time.  The function being displayed
  10.  *    is sin*cos.  The y-dimension displays the sine wave over time.  For each iteration,
  11.  *    the x-dimension shows a portion of a cosine wave which is multiplied by the sine wave.
  12.  *    Both the sine and cosine frequency (amount of wave or waves visible in the mesh) can
  13.  *    be changed.  Shown below are all the commands.
  14.  *
  15.  *    q    positive rotation about x-axis
  16.  *    w    negitive rotation about x-axis
  17.  *    a    positive rotation about y-axis
  18.  *    s    negitive rotation about y-axis
  19.  *    z    positive rotation about z-axis
  20.  *    x    negitive rotation about z-axis
  21.  *    e    zoom in
  22.  *    r    zoom out
  23.  *    d    increase sine frequency
  24.  *    f    decrease sine frequency
  25.  *    c    increase cosine frequency
  26.  *    v    decrease cosine frequency
  27.  *    space    quit
  28.  *       
  29.  * Copyright Info:
  30.  *    Copyright (C) 1993, 1994 -- by Michael P. Schenck, 
  31.  *    (mps4466@ultb.isc.rit.edu)
  32.  *    
  33.  *    This program is free software; you can redistribute it and/or modify
  34.  *    it under the terms of the GNU General Public License as published
  35.  *    by the Free Software Foundation; either version 2 of the License,
  36.  *    or (at your option) any later version.
  37.  *
  38.  *    This software is distributed in the hope that it will be useful, but
  39.  *    WITHOUT ANY WARRANTY; without even the implied warranty of
  40.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  41.  *    GNU General Public License for more details.
  42.  *
  43.  *      For a copy of the GNU General Public License
  44.  *    write to the Free Software Foundation, 675 Mass Ave,
  45.  *    Cambridge, MA  02139, USA.
  46.  *
  47.  */
  48.  
  49. #include <stdlib.h>
  50. #include "include/graphicsubsystem.h"
  51.  
  52. #define    SIZE    10    /* change SIZE to 20, change plane10.bin to plane20.bin and recompile.
  53.                You will get a larger mesh. */
  54.  
  55. void cleanexit(void);
  56.  
  57. UWORD colortable[2] = {0x007,0xFFF};    /* Rgb format (blue and white). */
  58.      
  59. UBYTE error,done,key;
  60. ULONG plane,cube;            /* Primitive IDs. */
  61. struct Action *pnode,*cnode;        /* Pointers to actions blocks in display map. */
  62. struct View *view = NULL;        /* Pointer to view structure. */
  63. MATRIX m;                  /* Pointers to matrix. */
  64. FLOAT j,x,y,
  65.       xrot=0.0,yrot=0.0,zrot=0.0,
  66.       freq1=0.3,freq2=0.3,
  67.       *vert;
  68. LONG i;
  69.  
  70. extern struct Model *models[MAXNUMMODELS];    /* we need access to model verticies */  
  71.  
  72. main ()
  73. {
  74.     /* Open a 640x400 screen with background blue and lines white and specify
  75.        a model library path. */
  76.  
  77.    if((error = opengraphics(640,400,colortable,"ModelLibrary/")) != SUCCESS)
  78.        return(error);
  79.     
  80.     /* Allocate a matrix that will be used for both the mesh and the cube. */ 
  81.     
  82.    if((m = allocatematrix()) == NULL)
  83.         cleanexit();
  84.    
  85.     /* Set up plane (mesh). */
  86.  
  87.    if((plane = requestmodel("plane10.bin",SINGLELINK)) == NOMODEL)
  88.         cleanexit();
  89.    if((pnode = createrootaction(m,plane)) == NULL)
  90.        cleanexit();     
  91.    
  92.        /* Set up cube for outline.  These models are all 'unit' models. */
  93.    
  94.    if((cube = requestmodel("cube.bin",SINGLELINK)) == NOMODEL)
  95.         cleanexit();
  96.    if((cnode = createrootaction(m,cube)) == NULL)
  97.        cleanexit();     
  98.  
  99.          /* Allocate and initialize a view structure. */
  100.    
  101.    view = allocateview();
  102.    view->x = 0.0;        /* X,Y,Z position looking at. */
  103.    view->y = 0.0;    
  104.    view->z = 0.0;
  105.    view->phi = 1.58;        /* Standard spherical coords of camera relative to that pos. */
  106.    view->theta = 0.0;        /* Thus, looking right down the x-axis toward neg direction. */
  107.    view->ro = 15.0;        /* Magnitude of vector depicted by above two values. */
  108.    view->d = 1.0;        /* Position of front clipping plane. */
  109.    view->f = 1000.0;        /* Position of far clipping plane. */
  110.    
  111.    configureview(view);
  112.  
  113.        /* Setup initial transformation.  Remember, we are using one matrix for both objects! */ 
  114.  
  115.    setsrttrans(4.0,4.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,m);
  116.  
  117.      /* Get a pointer to the vertices of the plane model. */
  118.  
  119.    vert = models[plane]->verticies;
  120.    
  121.        /* Loop until done is true. */
  122.    
  123.    done = FALSE;
  124.    
  125.    while(!done) {
  126.     
  127.     /* Tell system that a matrix has changed so that it will recompute the portion
  128.        of the display map below that point.  This is done for both objects. */
  129.  
  130.     pnode->changed = TRUE;    
  131.     cnode->changed = TRUE;
  132.     
  133.     /* Shift rows of z coords down (verticies are homogeneous, so there are four values). */
  134.     
  135.     for(i=((SIZE+1)*SIZE-1);i>=0;i--) 
  136.         *(vert+(i+SIZE+1)*4+2) = *(vert+i*4+2);
  137.         
  138.     /* Calculate new row. */
  139.         
  140.     j += freq1;    
  141.     y = sin(j);
  142.     x = 0.0;    
  143.     for(i=0;i<(SIZE+1);i++) {
  144.         *(vert+i*4+2) = y*cos(x);
  145.         x += freq2;     
  146.     }
  147.     
  148.     key = getinput(NOWAIT);    /* No wait will allow us to continue. */
  149.     
  150.     /* Check for input. */
  151.     
  152.     switch(key) {
  153.         case 'q' :    xrot += 0.1;    
  154.                 setsrttrans(4.0,4.0,4.0,xrot,yrot,zrot,0.0,0.0,0.0,m);
  155.                 break;
  156.         case 'w' :    xrot -= 0.1;
  157.                 setsrttrans(4.0,4.0,4.0,xrot,yrot,zrot,0.0,0.0,0.0,m);
  158.                 break;
  159.         case 'a' :    yrot += 0.1;
  160.                 setsrttrans(4.0,4.0,4.0,xrot,yrot,zrot,0.0,0.0,0.0,m);
  161.                 break;
  162.         case 's' :    yrot -= 0.1;
  163.                 setsrttrans(4.0,4.0,4.0,xrot,yrot,zrot,0.0,0.0,0.0,m);
  164.                 break;    
  165.         case 'z' :    zrot += 0.1;
  166.                 setsrttrans(4.0,4.0,4.0,xrot,yrot,zrot,0.0,0.0,0.0,m);
  167.                 break;
  168.         case 'x' :    zrot -= 0.1;
  169.                 setsrttrans(4.0,4.0,4.0,xrot,yrot,zrot,0.0,0.0,0.0,m);
  170.                 break;
  171.         case 'e' :    view->ro -= 0.25;
  172.                 configureview(view);
  173.                 break;
  174.         case 'r' :    view->ro += 0.25;
  175.                 configureview(view);
  176.                 break;
  177.         case 'd' :    freq1 += 0.01;
  178.                 break;
  179.         case 'f' :    freq1 -= 0.01;
  180.                 break;
  181.         case 'c' :    freq2 += 0.01;
  182.                 break;
  183.         case 'v' :    freq2 -= 0.01;
  184.                 break;
  185.         case ' ' :    done = TRUE;
  186.     }
  187.  
  188.     /* Update the screen with all the new changes. */
  189.  
  190.         displaygraphics();    
  191.    }
  192.    cleanexit();
  193. }
  194.  
  195. void cleanexit()
  196. {  
  197.        /* Free the matrix. */
  198.     
  199.    freematrix(m);
  200.     
  201.     /* Release the view structure. */
  202.     
  203.    if(view)
  204.       releaseview(view);
  205.    
  206.        /* Shutdown */
  207.     
  208.    closegraphics();
  209.    
  210.    exit(0);
  211. }   
  212.    
  213.    
  214.